﻿@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <label class="layui-form-label">菜单名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="menuname" placeholder="" class="layui-input">
                </div>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="menu-query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button type="reset" class="pear-btn pear-btn-md">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>

<div class="layui-card">
    <div class="layui-card-body">
        <table id="menu-table" lay-filter="menu-table"></table>
    </div>
</div>


<script type="text/html" id="menu-toolbar">

    @{ await Html.RenderPartialAsync("_ToolbarAuthTpl", new SystemFunctionModel
        {
            MenuKey = SysMenuConstant.MENU_MANAGE,
            FunDictionary = new Dictionary<string, string> { { "BatchExpand", "展开" }, { "BatchFold", "折叠" }, { "Add", "新增" }, { "Delete", "删除" },{ "Refresh","刷新" } }
        });
    }
</script>

<script type="text/html" id="menu-bar">
    @{
      await  Html.RenderPartialAsync("_OperationTpl", new SystemFunctionModel
        {
            MenuKey =SysMenuConstant.MENU_MANAGE,
            FunDictionary = new Dictionary<string, string> { { "Add", "新增下级" }, { "Update", "编辑" }, { "Delete", "删除" } }
        });
    }
</script>
<script type="text/html" id="icon">
    <i class="layui-icon {{d.iconCls}}"></i>
</script>
@section scripts{
    <script>
    var getfun = function (fcode) {
        var html = '';
        let fcodestr = ','+fcode + ',';
        for (var i = 0; i < funcodearry.length; i++) {
            if (fcodestr.indexOf(','+funcodearry[i].FunCode+',') > -1) {
                html += '<span class="layui-badge-rim">' + funcodearry[i].FunName + '</span>&nbsp;';
            }
        }
        return html;
    }
    var funcodearry = @Html.Raw(ViewBag.FunList);
        layui.use(['table', 'form', 'lscom', 'treetable'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let treetable = layui.treetable;
            let lscom = layui.lscom;
        let MODULE_PATH = "/Systems/Menu/";

        var istoolbar = $.trim($("#menu-bar").html())=="" ? true : false;
        treetable.render({
            treeColIndex: 1,
            treeSpid: 0,
            treeIdName: 'id',
            treePidName: 'pId',
            skin: 'line',
            treeDefaultClose: true,
            toolbar: '#menu-toolbar',
            elem: '#menu-table',
            url: MODULE_PATH+'GetAllMenu',
            page: false,
            cols: [
                [
                    { type: 'checkbox', width: 60 },
                    { field: "name", title: "菜单名称", width: 180 },
                    { field: "iconCls", title: "菜单图标", width: 100, templet: '#icon' },
                    { field: "menuCode", title: "菜单代码", width: 180 },
                    { field: "url", title: "菜单地址", width: 240 },
                    {
                        field: 'funcode', title: '所属功能', templet: function (d) {
                            return getfun(d.funcode);
                        }
                    },
                    { title: "操作", width: 180, align: "center", fixed: "right", templet: "#menu-bar",hide:istoolbar }
                ]
            ]
        });

        table.on('tool(menu-table)', function (obj) {
            if (obj.event === 'Delete') {
                window.remove(obj.data['id']);
            } else if (obj.event === 'Update') {
                window.edit(obj.data['id']);
            } else if (obj.event === 'Add') {
                window.add(obj.data['id']);
            }
        })
            
        table.on('toolbar(menu-table)', function (obj) {
            if (obj.event === 'Add') {
                window.add();
            } else if (obj.event ==='AddFun') {
                window.addfun();
            }
            else if (obj.event === 'Delete') {
                let checkData = table.checkStatus(obj.config.id).data;
                if (checkData.length === 0) {
                    parent.layer.msg("未选中数据", {
                        icon: 3,
                        time: 1000
                    });
                    return false;
                }
                var ids = checkData.map(function (d) { return d.id; });
                window.remove(ids.join(','));
            } else if (obj.event === 'BatchExpand') {
                treetable.expandAll("#menu-table");
            } else if (obj.event === 'BatchFold') {
                treetable.foldAll("#menu-table");
            } else if (obj.event === 'Refresh') {
                treetable.reload("#menu-table");
            }
        });

        form.on('submit(menu-query)', function (data) {
            var keyword = data.field.menuname;
            treetable.search('#menu-table', keyword);
            return false;
        });
        window.addfun=function(){
            parent.layer.open({
                    type: 2,
                    title: "新增功能",
                    content: MODULE_PATH +"CreateFunction",
                    area: ["710px","436px"],
                    shade: 0.1,
                    btn: ['确定', '取消'],
                    yes: function (index, layero) {
                        parent.window['layui-layer-iframe'+ index].submitForm();
                    }
                });
        }
           
            window.add = function (pid) {
                var conditaion = '';
                if (pid) {
                    conditaion = '?pid=' + pid;
                }
            parent.layer.open({
                type: 2,
                title: '新增菜单',
                shade: 0.1,
                area: ['750px', '700px'],
                content: MODULE_PATH + 'CreateMenu' + conditaion,
                btn: ['确定', '取消'],
                yes: function (index, layero) {
                    parent.window['layui-layer-iframe' + index].submitForm();
                }
            });
        }

            window.edit = function (id) {
            parent.layer.open({
                type: 2,
                title: '修改菜单',
                shade: 0.1,
                area: ['750px', '700px'],
                content: MODULE_PATH + 'EditMenu?id=' + id,
                btn: ['确定', '取消'],
                yes: function (index, layero) {
                    parent.window['layui-layer-iframe' + index].submitForm();
                }
            });
        }

        window.remove = function (data) {
            parent.layer.confirm('确定删除吗?', {
					icon: 3,
					title: '提示'
                }, function (index) {
                    parent.layer.close(index);
					let waitIndex = parent.layer.load(2);
                    lscom.ajax({
                        url: MODULE_PATH + 'Del',
                        data: JSON.stringify({ "ids": data }),
                        lsHandleError: false
                    }).done(function (data) {
                        if (data.code == 0) {
                            lscom.notify.success(data.message);
                            treetable.reload("#menu-table");
                        }
                    }).fail(function (message) {
                        lscom.notify.error(message);
                    }).always(function () {
                        parent.layer.close(waitIndex);
                    });
                });
        }

        window.saveCallback = function (data) {
            parent.layer.close(data.index);
            lscom.notify.success(data.message);
            treetable.reload("#menu-table");
        }
    })
       
    </script>
}

